<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Attitude_Detail_Results_Controller extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->helper('xlsimport/php-excel-reader/excel_reader2');
        $this->load->helper('xlsimport/spreadsheetreader.php');
        $this->load->model('attitudedetailresult', '', true);
        $this->load->model('user', '', true);

        if (!$this->session->userdata('logged_in')) {
            redirect('login');
        }

        $this->LoggedUser = $this->user->getLoggedUser();

        $this->load->library('authentication');
        $this->load->helper('t');
        $this->loggedUser = $this->authentication->getLoggedUser();
        $this->authentication->redirectIfNotBe(array('SS', 'KAS'));
    }

    function index_content($tlCode = 'all', $title = 'all') {
        $this->load->helper('form');

        $this->load->model('evaluation', '', true);
        $this->load->model('attitudedetailresult', '', true);

        //$titles = $this->user->get_enum_values();
        $this->load->model('region');
        $regions = $this->region->get_list();
        $regionDic = array();
        foreach ($regions as $key => $value) {
            $regionDic[$value['rsm_code']] = $value['region_name'];
        }
        $viewData['regionDic'] = $regionDic;

        $titles = array('NR' => 'NR', 'PG' => 'PG', 'BS' => 'BS', 'Medical' => 'Medical');
        $viewData['titles'] = $titles;

        $teamleaders = $this->user->get_list_by_title(array('NRTL', 'BSTL', 'PGTL', 'MedTL'), array('status' => 'active', 'supervisor_code' => $this->loggedUser->code));
        $viewData['teamleaders'] = $teamleaders;

        $users = array();
        if ($title != 'all') {
            $viewData['filterTitle'] = $title;
            switch ($title) {
                case 'NR':
                    $users = $this->user->get_list_by_title(array('NR', 'NRTL'), array('status' => 'active', 'supervisor_code' => $this->loggedUser->code));
                    break;
                case 'PG':
                    $users = $this->user->get_list_by_title(array('PG', 'PGTL'), array('status' => 'active', 'supervisor_code' => $this->loggedUser->code));
                    break;
                case 'BS':
                    $users = $this->user->get_list_by_title(array('BS', 'BSTL'), array('status' => 'active', 'supervisor_code' => $this->loggedUser->code));
                    break;
                case 'Medical': 
                    $users = $this->user->get_list_by_title(array('MedNR', 'MedBS', 'MedTL'), array('status' => 'active', 'supervisor_code' => $this->loggedUser->code));
                    
                    break;
            }
        } else {
            $users = $this->user->get_list_by_title(get_employee_titles(), array('status' => 'active', 'supervisor_code' => $this->loggedUser->code));
        }
        if ($tlCode != 'all') {
            $temp = array();
            foreach ($users as $eachUser) {
                if ($eachUser['teamleader_code'] == $tlCode) {
                    $temp[] = $eachUser;
                }
            }
            $users = $temp;
            $viewData['filterTL'] = $tlCode;
        }

        $viewData['rows'] = $users;

        $evaluation = $this->evaluation->get_last();
        $viewData['evaluationInfo'] = $evaluation;

//        $daDanhGia = 0;
//        $chuaDanhGia = 0;
        foreach ($viewData['rows'] as &$eachRow) {
            $adr = $this->attitudedetailresult->get_wheres(array('user_id' => $eachRow['id'], 'eid' => $evaluation['id']));
//            if($adr == false)
//            {
//                $chuaDanhGia++;
//            }
//            else
//            {
//                $daDanhGia++;
//            }
            $eachRow['attResult'] = $adr;
//            $eachRow['score'] = $adr['score'];
//            $eachRow['rating'] = $adr['rating'];
//            $eachRow['rated_day'] = $adr['rated_day'];
//            $eachRow['attResultStatus'] = $adr['status'];
            
        }
        
//        $viewData['daDanhGia'] = $daDanhGia;
//        $viewData['chuaDanhGia'] = $chuaDanhGia;
//        
//        $chartData = array();
//        $chartData[] = array('Loại', 'Số lượng');
//        $chartData[] = array('Đã đánh giá', $daDanhGia);
//        $chartData[] = array('Chưa đánh giá', $chuaDanhGia);        
//        $viewData['chartData'] = json_encode($chartData);

        return $viewData;
    }

    function index($tlCode = 'all', $title = 'all', $attRating = 'all') {
                
        $viewData = $this->index_content($tlCode, $title);
        
        $this->load->model('Settings');
        $settings = $this->Settings->get(1);
        $viewData['settings'] = $settings;
        

        $this->load->view(get_folder($this->loggedUser->title) . '/attitude_detail_results_list', $viewData);
    }

    function report($tlCode = 'all', $title = 'all', $attRating = 'all') {
        $viewData = $this->index_content($tlCode, $title);
        
        
        $this->load->model('AttitudeRating');
        $attRatings = $this->AttitudeRating->get_list();
        
        $arNote = array();
        $arDic = array();
        foreach ($attRatings as $eachRating) {
            $arNote[$eachRating['id']] = 0;
            $arDic[$eachRating['id']] = $eachRating['name'];
        }
        
        $users = $viewData['rows'];
        $numberDone = 0;
        $numberNot = 0;
        foreach ($users as &$eachUser) {
            // thống kê
            if ($eachUser['attResult'] == false) {
                $numberNot++;
            }
            else
            {
                $arNote[$eachUser['attResult']['art_id']]++;
                $numberDone++;
            }
            
            if ($attRating == 'done') {
                if ($eachUser['attResult'] == false) {
                    unset($users[array_search($eachUser, $users)]);
                    continue;
                }
            } else if ($attRating == 'not') {
                if ($eachUser['attResult'] != false) {
                    unset($users[array_search($eachUser, $users)]);
                    continue;
                }
            }
            else if($attRating != 'all')
            {
                if ($eachUser['attResult'] == false || ($eachUser['attResult'] != false && $eachUser['attResult']['art_id'] != $attRating)) {
                    unset($users[array_search($eachUser, $users)]);
                    continue;
                }
            }
        }
        $viewData['filterAtt'] = $attRating;
        $viewData['numberDone'] = $numberDone;
        $viewData['numberNot'] = $numberNot;
        $viewData['arNote'] = $arNote;
        $viewData['arDic'] = $arDic;
        
        $viewData['rows'] = $users;
        
        // vẽ biểu đồ
        $chartData = array();
        $chartData[] = array('Loại', 'Số lượng');
        foreach ($attRatings as $eachRating) {
            $chartData[] = array($arDic[$eachRating['id']], $arNote[$eachRating['id']]);
        }
        $viewData['chartData'] = json_encode($chartData);
        
        
        $viewData['attRatings'] = $attRatings;
        
        
        $this->load->model('Settings');
        $settings = $this->Settings->get(1);
        $viewData['settings'] = $settings;
        
        $this->load->view(get_folder($this->loggedUser->title) . '/attitude_detail_results_report', $viewData);
    }
    
    
    // có truyền adr_id => vừa click nút xác nhận một kết quả
    /*
    function do_confirm($adr_id = null, $status) {
        if ($adr_id != null) {
            $adr = $this->attitudedetailresult->get($adr_id);
            
            switch ($status) {
                case 'confirm':
                    $adr['status'] = 'confirmed';
                    break;
                case 'reject':
                    $adr['status'] = 'rejected';
                    break;
                default:
                    redirect(get_folder($this->loggedUser->title) . '/attitude_detail_results/confirm');
                    return; // trạng thái vớ vẩn thì thoát luôn
            }
            
            $adr['confirm_day'] = date('Y-m-d H:i:s');
            $this->attitudedetailresult->update($adr);
            
//            $employee = $this->user->get($adr['user_id']);
//            $ss = $this->user->get_user_by_code($employee['supervisor_code']);
//            if($status == 'reject')
//            {
//                send_mail_reject($ss['email'], $ss['name'], $employee);
//            }
            
            redirect(get_folder($this->loggedUser->title) . '/attitude_detail_results/confirm');
        }
    }
    */
    
    /*
    function do_multi_confirm() {
        $json = array('success' => 'ok', 'results' => array());

        $selecteds = $_POST['selecteds'];
        foreach ($selecteds as $eachAdrId) {
            $adr = $this->attitudedetailresult->get($eachAdrId);
            if ($adr['status'] == 'waiting' || $adr['status'] == 'review') {
                $adr['status'] = 'confirmed';
                $adr['confirm_day'] = date('Y-m-d H:i:s');
                $this->attitudedetailresult->update($adr);
            }
            // để ngoài if để đảm bảo đủ số lượng, phục vụ việc xử lý ở javascript
            $json['results'][] = $adr;
        }

        header("content-type:application/json");
        echo json_encode($json);
    }*/

    /*
    function confirm($ss_code = 'all', $title = 'all') {

        $this->load->helper('form');
        $this->load->model('evaluation', '', true);
        $this->load->model('attitudedetailresult', '', true);

        $this->load->model('region');
        $regions = $this->region->get_list();
        $regionDic = array();
        foreach ($regions as $key => $value) {
            $regionDic[$value['rsm_code']] = $value['region_name'];
        }
        $viewData['regionDic'] = $regionDic;


        $titles = array('NR' => 'NR', 'PG' => 'PG', 'BS' => 'BS', 'Medical' => 'Medical');
        $viewData['titles'] = $titles;

        $this->load->model('region');
        $supervisors = $this->user->get_list_by_title(array('SS'), array('rsm_code' => $this->loggedUser->code));
        //$regions = array('HCM', 'MEK', 'HNO', 'CEN', 'EAS');
        $viewData['supervisors'] = $supervisors;

        $users = array();
        if ($title != 'all') {
            $viewData['filterTitle'] = $title;
            switch ($title) {
                case 'NR':
                    $users = $this->user->get_list_by_title(array('NR', 'NRTL'), array('rsm_code' => $this->loggedUser->code));
                    break;
                case 'PG':
                    $users = $this->user->get_list_by_title(array('PG', 'PGTL'), array('rsm_code' => $this->loggedUser->code));
                    break;
                case 'BS':
                    $users = $this->user->get_list_by_title(array('BS', 'BSTL'), array('rsm_code' => $this->loggedUser->code));
                    break;
            }
        } else {
            $users = $this->user->get_list_by_title(get_employee_titles(), array('rsm_code' => $this->loggedUser->code));
        }
        if ($ss_code != 'all') {
            $temp = array();
            foreach ($users as $eachUser) {
                if ($eachUser['supervisor_code'] == $ss_code) {
                    $temp[] = $eachUser;
                }
            }
            $users = $temp;
            $viewData['filterSS'] = $ss_code;
        }

        //$users = $this->user->get_list(array('rsm_code' => $loggedUser->code));
        $temp = array();
        foreach ($users as $eachUser) {
            if ($eachUser['title'] != 'SS') {
                $temp[] = $eachUser;
            }
        }
        $viewData['rows'] = $temp;
        $evaluation = $this->evaluation->get_last();
        $viewData['evaluationInfo'] = $evaluation;

        foreach ($viewData['rows'] as &$eachRow) {
            $adr = $this->attitudedetailresult->get_wheres(array('user_id' => $eachRow['id'], 'eid' => $evaluation['id']));
            $eachRow['adr_id'] = $adr['id'];
            $eachRow['score'] = $adr['score'];
            $eachRow['rating'] = $adr['rating'];
            $eachRow['rated_day'] = $adr['rated_day'];
            $eachRow['confirm_day'] = $adr['confirm_day'];
            $eachRow['confirmStatus'] = $adr['status'];
        }

        $this->load->view(get_folder($this->loggedUser->title) . '/attitude_detail_results_confirm', $viewData);
    }*/

    function add() {

        $this->load->model('evaluation');
        $evaluation = $this->evaluation->get_last();

        $this->load->model('attituderesult');
        $viewData['evaluation'] = $evaluation;

        $viewData['users'] = $this->user->user_list('0');

        if ($this->input->post('Command') && $this->input->post('Command') == 'Add') {
            // Thực hiện form validation

            $newRow = array(
                'user_id' => $this->input->post('user_id'),
                'eid' => $evaluation['id']
            );

            $viewData['addResult'] = $this->attitudedetailresult->insert($newRow);
        }


        $this->load->view(get_folder($this->loggedUser->title) . '/attitude_detail_result_add', $viewData);
    }

    function edit($id) {

//        $logged_in = $this->session->userdata('logged_in');
//        if ($logged_in['su'] != "1") {
//            exit('Permission denied');
//            return;
//        }
//        $this->load->model('category', '', TRUE);
//        $viewData['category'] = $this->category->category_dropdown();
//
//        $this->load->model('difficult_level', '', TRUE);
//        $viewData['difficult_level'] = $this->difficult_level->level_dropdown();

        if ($this->input->post('Command') && $this->input->post('Command') == 'Edit') {
            //print_r($_POST);

            $newAttitudeResult = array(
                'id' => $id,
                'name' => $this->input->post('name')
            );

            $viewData['editResult'] = $this->attitudeuserdetail->update($newAttitudeResult);
        }

        $viewData['attitudeResult'] = $this->attitudeuserdetail->get($id);

//        $viewData['title'] = "Edit question";

        $this->load->view(get_folder($this->loggedUser->title) . '/attitude_user_detail_edit', $viewData);
    }

    function delete($id) {
//        $logged_in = $this->session->userdata('logged_in');
//        if ($logged_in['su'] != "1") {
//            exit('Permission denied');
//            return;
//        }
        $viewData['deleteResult'] = $this->attitudeuserdetail->delete($id);

        redirect(get_folder($this->loggedUser->title).'/attitude_user_details', 'refresh');
    }

}